﻿using Microsoft.Win32;
using System;
using System.ComponentModel;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Resources;

namespace TestApp
{
    public partial class MainWindow : Window
	{
		public MainWindow()
		{
			InitializeComponent();

            PreviewKeyDown += MainWindow_KeyDown;
        }

        private void MainWindow_KeyDown(object sender, KeyEventArgs e)
        {
            if (Singleton.Instance().isInTextBox)
                return;

            if (Keyboard.Modifiers == ModifierKeys.Control && e.Key == Key.S)
            {
                save_file(null, null);
                e.Handled = true;
            }
            else if (Keyboard.Modifiers == ModifierKeys.Control && e.Key == Key.N)
            {
                new_file(null, null);
                e.Handled = true;
            }
            else if (Keyboard.Modifiers == ModifierKeys.Control && e.Key == Key.O)
            {
                open_file(null, null);
                e.Handled = true;
            }
            else if (Keyboard.Modifiers == ModifierKeys.Control && e.Key == Key.C)
            {
                flowchat.copy();
                e.Handled = true;
            }
            else if (Keyboard.Modifiers == ModifierKeys.Control && e.Key == Key.V)
            {
                flowchat.paste();
                e.Handled = true;
            }            
            else if (Keyboard.Modifiers == ModifierKeys.Control && e.Key == Key.Z)
            {
                flowchat.undo();
                e.Handled = true;
            }
            else if (e.Key == Key.Up)
            {
                flowchat.moveUp(1);
                e.Handled = true;
            }
            else if (e.Key == Key.Down)
            {
                flowchat.moveUp(-1);
                e.Handled = true;
            }
            else if (e.Key == Key.Left)
            {
                flowchat.moveLeft(1);
                e.Handled = true;
            }
            else if (e.Key == Key.Right)
            {
                flowchat.moveLeft(-1);
                e.Handled = true;
            }

        }

        private void new_file(object sender, RoutedEventArgs e)
        {
            flowchat.new_file();

            flowchat.updateTabIteName("未命名");
        }

        private void open_file(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            //过滤文件类型
            openFileDialog.Filter = "文档(*.jianqiang)|*.jianqiang|所有文件(*.*)|*.*";

            //允许多选
            openFileDialog.Multiselect = false;

            string strModel = null;
            if (openFileDialog.ShowDialog() == true)
            {
                //FileName 完整路径：相对路径+文件名+后缀
                //文件内容读取
                StreamReader sr = File.OpenText(openFileDialog.FileName);
                strModel = sr.ReadLine();
                flowchat.loadModel(strModel);
                sr.Dispose();
                sr.Close();
            }

            flowchat.updateTabIteName(openFileDialog.FileName);
        }

        private void save_file(object sender, RoutedEventArgs e)
        {
            if (Singleton.Instance().save_file_name == null)
            {
                SaveFileDialog save = new SaveFileDialog();
                save.Filter = "文档(*.jianqiang)|*.jianqiang|所有文件(*.*)|*.*";
                //保存类型文本文档或其他所有类型写法
                if (save.ShowDialog() == true)
                {
                    string filepath = save.FileName;

                    StreamWriter sw = new StreamWriter(filepath);

                    String content = flowchat.saveModel();

                    sw.WriteLine(content);
                    sw.Flush();
                    sw.Close();

                    Singleton.Instance().save_file_name = filepath;

                    flowchat.updateTabIteName(filepath);
                }
            } else
            {
                StreamWriter sw = new StreamWriter(Singleton.Instance().save_file_name);

                String content = flowchat.saveModel();

                sw.WriteLine(content);
                sw.Flush();
                sw.Close();
            }
        }

        private void save_as_file(object sender, RoutedEventArgs e)
        {
            SaveFileDialog save = new SaveFileDialog();
            save.Filter = "文档(*.jianqiang)|*.jianqiang|所有文件(*.*)|*.*";
            //保存类型文本文档或其他所有类型写法
            if (save.ShowDialog() == true)
            {
                string filepath = save.FileName;

                StreamWriter sw = new StreamWriter(filepath);

                String content = flowchat.saveModel();

                sw.WriteLine(content);
                sw.Flush();
                sw.Close();

                Singleton.Instance().save_file_name = filepath;

                flowchat.updateTabIteName(filepath);
            }
        }

        private void open_template_file(object sender, RoutedEventArgs e)
        {
            Uri uri = new Uri("pack://application:,,,/Resources/cnn.manyun", UriKind.Absolute);

            StreamResourceInfo info = Application.GetResourceStream(uri);
            //读取资源文件
            StreamReader reader = new StreamReader(info.Stream, Encoding.UTF8);
            String content = reader.ReadToEnd();
            reader.Close();

            flowchat.loadModel(content);

            flowchat.updateTabIteName("未命名");
        }
    }
}
